<template>
    <div>

        <el-input v-model="query.workName" style="width: 200px" placeholder="请输入作业名"></el-input>
        <el-input v-model="query.workTeacher" style="width: 200px"  placeholder="请输入老师"></el-input>
        <el-button @click="getList()" type="primary">查询</el-button>
        <el-button @click="handleCreate()" type="primary" v-if="this.status == '1'">新增</el-button>

        <el-table
                :data="HomeworkData"
                style="width: 100%">
            <el-table-column
                    prop="workName"
                    label="作业名"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="workTeacher"
                    label="老师">
            </el-table-column>
            <el-table-column
                    prop="workContent"
                    label="作业内容">
            </el-table-column>
            <el-table-column
                    prop="workAsk"
                    label="作业要求">
            </el-table-column>
            <el-table-column label="操作" v-if="this.status == '2'">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">做作业</el-button>
<!--                    <el-button-->
<!--                            size="mini"-->
<!--                            type="danger"-->
<!--                            @click="handleDelete(scope)">删除</el-button>-->
                </template>
            </el-table-column>
            <el-table-column label="操作" v-else-if="this.status == '1'">
                <template slot-scope="scope">
<!--                    <el-button-->
<!--                            size="mini"-->
<!--                            @click="handleEdit(scope.row)">做作业</el-button>-->
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="this.query.page"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="this.query.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="this.total">
        </el-pagination>


        <el-dialog title="布置作业" :visible.sync="addDialogVisibe">
            <el-form :model="homeworkform">
                <el-form-item label="作业名" >
                    <el-input v-model="homeworkform.workName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="老师" >
                    <el-input v-model="homeworkform.workTeacher" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="作业要求" >
                    <el-input v-model="homeworkform.workAsk" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisibe = false">取 消</el-button>
                <el-button type="primary" @click="submitHomeworkForm()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="做作业" :visible.sync="dialogFormVisible">
            <el-form :model="homeworkform">
                <el-form-item label="姓名" >
                    <el-input v-model="homeworkform.workStu" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="老师" >
                    <el-input v-model="homeworkform.workTeacher" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="作业名称" >
                    <el-input v-model="homeworkform.workName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="作业要求" >
                    <el-input v-model="homeworkform.workAsk" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="作业内容" >
                    <el-input v-model="homeworkform.workContent" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm()">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "HomeworkList",
        data() {
            return {
                HomeworkData: [],
                total:0,
                query: {
                    page:1,
                    size:5,
                    workTeacher:"",
                    workName:"",
                },
                dialogFormVisible: false,
                addDialogVisibe:false,
                status : JSON.parse(sessionStorage.getItem("status")),
                dostatus : status,
                delstatus : status,
                homeworkform: {
                    id:'',
                    workName:"",
                    workTeacher:'',
                    workContent:'',
                    workAsk:'',
                    workStu:'',
                },
                dohomeworkform: {
                    did: "",
                    workNamedo:"",
                    workTeacherdo:'',
                    workContentdo:'',
                    workAskdo:'',
                    workStudo:'',
                },
            }
        },
        mounted() {
            this.getList();
        },
        methods:{
            getList(){
                this.axios.get('http://localhost:3312/homework/getHomeworkList',{
                    params:{
                        page:this.query.page,
                        size:this.query.size,
                        name:this.query.name,
                        workName:this.query.workName,
                        workTeacher:this.query.workTeacher,
                    }
                }).then((resp)=>{
                    this.HomeworkData = resp.data.content.list;
                    this.total = resp.data.content.total;
                })
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.query.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.query.page = val;
                this.getList();
            },
            handleEdit(row){
                this.homeworkform = row;
                this.dialogFormVisible = true;
            },
            handleCreate(){
                this.dohomeworkform = {};
                this.dialogFormVisible = false;
                this.addDialogVisibe = true;
            },
            handleDelete(scope){
                let id = scope.row.id;
                this.axios.delete('http://localhost:3312/homework/deleteHomework?id='+id).then((resp)=>{
                    let data = resp.data;
                    if (data.success){
                        this.getList();
                        this.$message({
                            message:'恭喜你,删除成功',
                            type:'success',
                        });
                    }
                })
            },
            submitForm(){
                this.dohomeworkform.workNamedo = JSON.parse( JSON.stringify(this.homeworkform.workName))
                this.dohomeworkform.workTeacherdo = JSON.parse( JSON.stringify(this.homeworkform.workTeacher))
                this.dohomeworkform.workStudo = JSON.parse( JSON.stringify(this.homeworkform.workStu))
                this.dohomeworkform.workContentdo = JSON.parse( JSON.stringify(this.homeworkform.workContent))
                this.dohomeworkform.workAskdo = JSON.parse( JSON.stringify(this.homeworkform.workAsk))
                console.log(this.dohomeworkform)
            this.axios.post('http://localhost:3312/homework/doHomeWork/',this.dohomeworkform).then((resp)=>{
                let data = resp.data;
                if (data.success){
                    this.dialogFormVisible = false;
                    this.homeworkform = {};
                    this.getList();
                    this.$message({
                        message:"操作成功",
                        type:'success'
                    });
                }
            })
            },
            submitHomeworkForm(){
                this.axios.post('http://localhost:3312/homework/saveHomework/',this.homeworkform).then((resp)=>{
                    let data = resp.data;
                    if (data.success){
                        this.addDialogVisibe = false;
                        this.homeworkform = {};
                        this.getList();
                        this.$message({
                            message:"操作成功",
                            type:'success'
                        });
                    }
                })
            }
        },
    }
</script>

<style scoped>

</style>